//
//  RecommendCollectionViewCell.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/6.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "RecommendCollectionViewCell.h"

@interface RecommendCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate, HotRecommendTableViewcellDelegate>

@property (nonatomic, strong)CarouselView *headerView;
@property (nonatomic, strong)NSMutableArray *hotRecommendsModelArray;
@property (nonatomic, strong)NSMutableArray *editorRecommendArray;
@property (nonatomic, strong)MBProgressHUD *hud;

@end


@implementation RecommendCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
        [self getNetData];
//        [self addheader];
        self.hud = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
        self.hud.color = [UIColor blackColor];
        self.hud.dimBackground = YES;
        self.hud.labelText = @"正在加载";
    }
    return self;
}

//自定义cell - tableview之间的协议

- (void)passValue:(HotRecommendListModel *)hotRecommendListModel {
    
    [self.delegate passValue:hotRecommendListModel];
    
    
}


#pragma -- 下拉刷新
//- (void)addheader {
//    __weak typeof(self) block = self;
//    [self.tableView addHeaderWithCallback:^{
//        [WXCNetWorkTool getWithUrl:FoundRecommendURL parameter:nil httpHeader:nil responseType:ResponseTypeJson success:^(id result) {
//            MainRecommendModel *mainRecommendModel = [[MainRecommendModel alloc] initWithDictionary:result];
//            block.hotRecommendsModelArray = [NSMutableArray array];
//            block.editorRecommendArray = [NSMutableArray array];
//            //热门MOdel
//            for (NSDictionary *dic in mainRecommendModel.hotRecommends[@"list"]) {
//                HotRecommendsModel *hotRecommendsModel = [[HotRecommendsModel alloc] initWithDictionary:dic];
//                [block.hotRecommendsModelArray addObject:hotRecommendsModel];
//            }
//            [block.tableView reloadData];
//            [block.tableView headerEndRefreshing];
//        } fail:^(NSError *error) {
//            
//        }];
//    }];
//}





//上拉加载







- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStyleGrouped];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[HotRecommendTableViewCell class] forCellReuseIdentifier:@"HotRecommendTableViewCellIdentifier"];
    [self addSubview:self.tableView];
    
    self.headerView = [[CarouselView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 150)];

    self.tableView.tableHeaderView = self.headerView;

    
}

- (void)getNetData {
    [WXCNetWorkTool getWithUrl:FoundRecommendURL parameter:nil httpHeader:nil responseType:ResponseTypeJson success:^(id result) {
        MainRecommendModel *mainRecommendModel = [[MainRecommendModel alloc] initWithDictionary:result];
        self.hotRecommendsModelArray = [NSMutableArray array];
        self.editorRecommendArray = [NSMutableArray array];
        self.headerView.dataSourceArray = [NSMutableArray array];
        [self.headerView.dataSourceArray addObjectsFromArray: [mainRecommendModel.focusImages valueForKey:@"list"]];
        
        //给轮播图传数据
        
        [self.headerView whenHasDataCarouselCollectionOpeation];
       


        //热门MOdel
        for (NSDictionary *dic in mainRecommendModel.hotRecommends[@"list"]) {
            HotRecommendsModel *hotRecommendsModel = [[HotRecommendsModel alloc] initWithDictionary:dic];
            [self.hotRecommendsModelArray addObject:hotRecommendsModel];
        }
        [self.tableView reloadData];
        [self.hud hide:YES afterDelay:2];
    } fail:^(NSError *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.hotRecommendsModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HotRecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HotRecommendTableViewCellIdentifier"];
    cell.hotRecommendModel = self.hotRecommendsModelArray[indexPath.row];
    cell.delegate = self;
    
    
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}




@end
